<?php
defined('_XR_EXEC') or die();
/**
 * 逻辑处理类 
 */
class report_order_Model extends report_base_model
{
 
	/* 田园卡订单出货单 */
	public function order1($formdata){
		$db = factory::getdbo();
 	  #查询条件
		$sql_where = ' where a.status in ('.$formdata['status'].') ';
		if($formdata['b_date'] != ''){
			$sql_where .= ' and a.rec_date>="'.$formdata['b_date'].'" ';
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and a.rec_date<="'.$formdata['e_date'].'"';	
		}
		return $this->orderhtml($sql_where);
	}	
	/* 田园卡订单出货单 */
	public function order2($formdata){
		$db = factory::getdbo();
 	  #查询条件
		$sql_where = ' where a.code="'.$formdata['code'].'" ';
		return $this->orderhtml($sql_where);
	}
	
	public function orderhtml($sql_where){
		$db = factory::getdbo();
		$sql = "
		select
		a.id, 
		a.code,
		a.rec_date,
		a.price_all,
		a.price_deliver,
		a.price_total,
		a.point_total,
		a.point_balance,
		a.vege_nums,
		a.trans_balance,
		a.created,
		b.username,
		b.name as user_name,
		b.sendaddr,
		b.sendman,
		b.sendphone,
		b.area,
		b.sendremark,
		c.val as zone,
		d.val as sendtime
		from `t_order` a 
		left join t_user b on a.user_id=b.id  
		left join t_list_zone c on b.zone =c.id
		left join t_list_sendtime d on a.sendtime=d.id
		".$sql_where."
		order by a.user_id,a.created
		" ;
		$rs_send= $db->loadassoclist($sql);
		if(!$rs_send){
			exit('没有找到记录');
		}
		foreach($rs_send as $k=>$v){
			$rs_send[$k]['goods'] = $db->loadassoclist('select id,combo_name,combo_type,price,nums,price_total from t_order_goods where order_id="'.$v['id'].'"  ');
			foreach($rs_send[$k]['goods'] as &$dz_val){
				$dz_val['items'] = $db->loadassoclist(' 
				select 
					b.name as vege_name,
					b.code as vege_code,
					c.type_name as vege_type_name,
					d.logo_name as vege_logo_name,
					a.vege_price,
					a.vege_num,
					c.show_index
				from t_order_goods_item a 
				left join t_vege b on a.vege_id=b.id 
				left join t_vegetype c on b.type_id=c.id
				left join t_vegelogo d on b.logo_id=d.id
				where a.order_goods_id="'.$dz_val['id'].'" order by c.show_index  ');
			}
		}
		 
		
		$s = ''; 
		$min_row = 12;
		$r_cungen =  array(
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">订单号：</td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">   </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">质检员：</td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">   </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">发货员：</td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">   </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9">配送员：</td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9"> </td>',
			'<td colspan="1" style="width:150px;background-color:#FDE9D9;font-size:16px">会员签收：</td>',
			'<td colspan="1" style="width:150px;border:0;background-color:#FDE9D9"> </td>'
		);
		$s_cungen_over = '<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>';
		
		foreach($rs_send as $k=>$v){
		// debug::show($v);
	    $s .= '
			<table border="1">
				<tr>
					<td colspan="10" style="height:50px;text-align:center;font-size:16px;font-weight:bold;border-right:0">田园会|会员出货单</td> 
					<td colspan="1" style="border:0;background-color:#FDE9D9" > </td>
				</tr>
				<tr>
					<td colspan="7"  > </td> 
					<td colspan="1"  >订单号：</td>
					<td colspan="2" style="vnd.ms-excel.numberformat:@" >'.$v['code'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9" > </td>
				</tr> 
				<tr>
					<td colspan="2" style="background-color:#C0C0C0;font-size:14px">会员姓名：</td>
					<td colspan="2" style="font-size:14px">'.$v['user_name'].'</td>
					<td colspan="2" style="background-color:#C0C0C0;font-size:14px">会员编号：</td>
					<td colspan="1" style="vnd.ms-excel.numberformat:@;font-size:14px">'.$v['username'].'</td>
					<td colspan="1" style="background-color:#C0C0C0;font-size:14px">配送日期：</td>
					<td colspan="2" style="">'.$v['rec_date'].' '.$v['sendtime'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9" > </td>
				</tr>
				<tr>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:30px;">序号</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:50px;">物料编码</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:50px;">品牌</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:100px;">套餐名称</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:60px;">品类</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:100px;">菜品</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:50px;">单价</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:70px;">份数</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:70px;">金额（元）</td>
					<td colspan="1" style="text-align:center;background-color:#008000;color:#ffffff;width:100px;">备注</td>
					<td colspan="1" style="background-color:#FDE9D9;width:150px;font-weight:bold">公司存根单</td>
				</tr>';
			$now_sendvege_id = 1;
		 
			foreach($v['goods'] as $key=>$val){
				$index_items = 0;
				foreach($val['items'] as $vv){
					$s .= '
					<tr>
						<td colspan="1" style="text-align:center">'.$now_sendvege_id.'</td>
						<td colspan="1" style="text-align:center">'.$vv['vege_code'].'</td>
						<td colspan="1" style="text-align:center">'.$vv['vege_logo_name'].'</td>
						<td colspan="1" style="text-align:center">'.$val['combo_name'].'</td>
						<td colspan="1" style="text-align:center">'.$vv['vege_type_name'].'</td>
						<td colspan="1" style="text-align:center;font-size:16px">'.$vv['vege_name'].'</td>
						<td colspan="1" style="text-align:center">'.$vv['vege_price'].'</td>
						<td colspan="1" style="text-align:center;font-size:16px">'.($vv['vege_num']*$val['nums']).'</td>';
					if($index_items == 0){
						$s .= '<td colspan="1" rowspan="'.count($val['items']).'" style="width:150px;text-align:right">'.$val['price_total'].'</td>';
					}
					$s .= '<td colspan="1"  > </td>';
					if( $now_sendvege_id > count($r_cungen) ){
						$s .= $s_cungen_over;
					}else{
						if(($now_sendvege_id - 1) == 1){
							$s .= '<td colspan="1"  style="vnd.ms-excel.numberformat:@;width:150px;background-color:#FDE9D9">'.$v['code'].'</td>';
						}else{
							$s .= $r_cungen[($now_sendvege_id - 1)];
						}
					}
					$s .='
					</tr>				
					';
					$index_items++;
					$now_sendvege_id++;
				}
			}
			 
			if($now_sendvege_id < $min_row){
				$shengyu_fornum = $min_row - $now_sendvege_id;
				for($ii=0; $ii<$shengyu_fornum; $ii++){
					$s .= '
					<tr>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>
						<td colspan="1">&nbsp;</td>';
					if( $now_sendvege_id > count($r_cungen) ){
						$s .= $s_cungen_over;
					}else{
						//$s .= $r_cungen[($now_sendvege_id - 1)];

						if(($now_sendvege_id - 1) == 1){
							$s .= '<td colspan="1" style="vnd.ms-excel.numberformat:@;width:150px;background-color:#FDE9D9">'.$v['code'].'</td>';
						}else{
							$s .= $r_cungen[($now_sendvege_id - 1)];
						}
						
						
					}
					$s .= '
					</tr>		
					';
					$now_sendvege_id++;
				}
			}
			
			$s .= '
			<tr>
				<td colspan="7" style="text-align:right">菜品合计</td>
				<td colspan="1" style="text-align:right">'.$v['vege_nums'].'</td>
				<td colspan="1" style="text-align:right">'.$v['price_all'].'</td>
				<td colspan="1"> </td>
				<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
			</tr>';
			$s .= '
			<tr>
				<td colspan="7" style="text-align:right">运费</td>
				<td colspan="1"> </td>
				<td colspan="1" style="text-align:right">'.$v['price_deliver'].'</td>
				<td colspan="1"> </td>
				<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
			</tr>';
			$s .= '
			<tr>
				<td colspan="7" style="text-align:right">总计</td>
				<td colspan="1"> </td>
				<td colspan="1" style="text-align:right">'.$v['price_total'].'</td>
				<td colspan="1"> </td>
				<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
			</tr>';
			
			
			$s .= '
				<tr>
					<td colspan="3" style="background-color:#C0C0C0">本次消费：</td>
					<td colspan="2" style="text-align:left;">'.$v['price_total'].'元</td>
					<td colspan="3" style="background-color:#C0C0C0">当前余额：</td>
					<td colspan="2" style="text-align:left;">'.$v['trans_balance'].'元</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="3" style="background-color:#C0C0C0">本次积分：</td>
					<td colspan="2" style="text-align:left;">'.$v['point_total'].'分</td>
					<td colspan="3" style="background-color:#C0C0C0">当前积分：</td>
					<td colspan="2" style="text-align:left;">'.$v['point_balance'].'分</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="3" style="background-color:#C0C0C0">所属区域：</td>
					<td colspan="2" style="text-align:left;">'.$v['area'].'</td>
					<td colspan="3" style="background-color:#C0C0C0">小区名称：</td>
					<td colspan="2" style="text-align:left;">'.$v['zone'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="3" style="background-color:#C0C0C0">配送时间/特殊说明：</td>
					<td colspan="7" style="text-align:left;">'.$v['sendremark'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="3" style="background-color:#C0C0C0">收货地址：</td>
					<td colspan="7" style="text-align:left;">'.$v['sendaddr'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
				<tr>
					<td colspan="3" style="background-color:#C0C0C0">联系人：</td>
					<td colspan="2" style="text-align:left;">'.$v['sendman'].'</td>
					<td colspan="3" style="background-color:#C0C0C0">联系电话：</td>
					<td colspan="2" style="text-align:left;">'.$v['sendphone'].'</td>
					<td colspan="1" style="border:0;background-color:#FDE9D9"> </td>
				</tr>
			</table>			
			<table border="0">
				<tr><td style="border:0">&nbsp;</td></tr>
			</table>
			';
			
		}
		return $s;
	}
   
	/*   */
	public function order3($formdata){
		$db = factory::getdbo();
		if(!isset($formdata['b_date']))    $formdata['b_date']  = ''; 
		if(!isset($formdata['e_date']))    $formdata['e_date']  = '';  
		 
		$sql_where = ' and b.status in ('.$formdata['status'].') ';
		 
		if($formdata['b_date'] != ''){
			$sql_where .= ' and b.rec_date>="'.$formdata['b_date'].'" ';
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and b.rec_date<="'.$formdata['e_date'].'"';	
		}
		$rsc = $db->loadassoclist('
		select  
	  a.vege_id,
		a.vege_num, 
		d.name as vege_name,
		c.nums,
		f.logo_name,
		g.type_name as vege_type_name
		from t_order_goods_item a 
		left join t_order b on a.order_id=b.id 
		left join t_order_goods c on a.order_goods_id=c.id  
		left join t_vege d on a.vege_id=d.id
		left join t_vegelogo f on d.logo_id=f.id
		left join t_vegetype g on d.type_id=g.id
		where 1=1 '.$sql_where.'  order by b.rec_date ');	
		
		$rs = array();
		foreach($rsc as $k=>$v){
			if(!isset($rs[$v['vege_id']])){
				$rs[$v['vege_id']] = array(
					'logo_name'=>$v['logo_name'],
					'vege_type_name'=>$v['vege_type_name'],
					'vege_name'=>$v['vege_name'],
					'vege_nums'=>($v['vege_num']*$v['nums'])
				);
			}else{
				$rs[$v['vege_id']]['vege_nums'] += $v['vege_num']*$v['nums'];
			}
		} 
		 
		
		$s = '
		<table border="1">
			<thead>
				<tr>
					<th>序号</th>
					<th>品牌</th> 
					<th>种类</th> 
					<th>菜品</th> 
					<th>份数</th>
				</tr>
			</thead>
			<tbody>
		';
		$k=1;
		foreach($rs as $v){
			$s .= '
				<tr>
					<td>'.($k++).'</td>  
					<td>'.$v['logo_name'].'</td>
					<td>'.$v['vege_type_name'].'</td>
					<td>'.$v['vege_name'].'</td>
					<td>'.$v['vege_nums'].'</td>
				</tr>
			';
		} 
		$s .= '
			</tbody>
		</table>
		';
		return $s; 
	}	   
	/*   */
	public function order4($formdata){
		$db = factory::getdbo();
		if(!isset($formdata['b_date']))    $formdata['b_date']  = ''; 
		if(!isset($formdata['e_date']))    $formdata['e_date']  = '';  
		 
		$sql_where = ' and b.status in ('.$formdata['status'].') ';
		 
		if($formdata['b_date'] != ''){
			$sql_where .= ' and b.rec_date>="'.$formdata['b_date'].'" ';
		}
		if($formdata['e_date']!=''){
			$sql_where .= ' and b.rec_date<="'.$formdata['e_date'].'"';	
		}
		$rs = $db->loadassoclist('
		select  
	  b.rec_date,
		b.code,
		b.status,
		e.username,
		e.name,
		a.vege_num,
		c.nums,
		d.name as vege_name,
		f.logo_name,
		g.type_name as vege_type_name
		from t_order_goods_item a 
		left join t_order b on a.order_id=b.id 
		left join t_order_goods c on a.order_goods_id=c.id
		left join t_user e on b.user_id=e.id
		left join t_vege d on a.vege_id=d.id
		left join t_vegelogo f on d.logo_id=f.id
		left join t_vegetype g on d.type_id=g.id
		where 1=1 '.$sql_where.'  order by b.rec_date ');	
		$s = '
		<table border="1">
			<thead>
				<tr>
					<th>序号</th>
					<th>配送日期</th>
					<th>订单号</th>
					<th>订单状态</th>
					<th>会员编号</th>
					<th>姓名</th> 
					<th>品牌</th> 
					<th>种类</th> 
					<th>菜品</th> 
					<th>份数</th>
				</tr>
			</thead>
			<tbody>
		';
		foreach($rs as $k=>$v){
			$s .= '
				<tr>
					<td>'.($k+1).'</td>
					<td>'.$v['rec_date'].'</td>
					<td  style="vnd.ms-excel.numberformat:@" >'.$v['code'].'</td>
					<td>';
			switch($v['status']){
				case "-2":
					$s .= '系统取消';
					break;
				case "-1":
					$s .= '用户取消';
					break;	
				case "0":
					$s .= '等待支付';
					break;	
				case "1":
					$s .= '等待审核';
					break;	
				case "2":
					$s .= '已审核，等待生成';
					break;	
				case "3":
					$s .= '已生产，等待配送';
					break;	
				case "4":
					$s .= '已配送';
					break;
			}
			$s .='</td>
					<td>'.$v['username'].'</td>
					<td>'.$v['name'].'</td>
					<td>'.$v['logo_name'].'</td>
					<td>'.$v['vege_type_name'].'</td>
					<td>'.$v['vege_name'].'</td>
					<td>'.($v['vege_num']*$v['nums']).'</td>
				</tr>
			';
		} 
		$s .= '
			</tbody>
		</table>
		';
		return $s; 
	}	
}

?>